<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\UploadFileRequest;
use Illuminate\Support\Facades\File;
use App\Services\UploadsManager;
use App\Services\UploadClass;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManagerStatic as Image;
use GuzzleHttp\Client;


use App\Model\Checkaccord;
use App\Model\Checkgroup;
use Excel;
use DB;

class UploadController extends Controller
{
    //
    protected $manager;

    public function __construct(UploadsManager $manager)
    {
        $this->manager = $manager;
    }

    /**
     * Show page of files / subfolders
     */
    public function index(Request $request)
    {
        $folder = $request->get('folder');
        $data = $this->manager->folderInfo($folder);

        return view('check.upload.index',$data);
    }

    /**
	 * 上传文件
	 */
	public function uploadExcel(UploadFileRequest $request)
	{
		set_time_limit(0);
	    $file = $_FILES['file'];
	    $fileName = $request->get('file_name');
	    $fileName = $fileName ?: $file['name'];
	    $path = str_finish($request->get('folder'), '/') . $fileName;
	   	$data = [];
	    $content = File::get($file['tmp_name']);
	    $filePath = $file['tmp_name'];
	    // $filePath = './public/word/test.xlsx';
	    Excel::load($filePath, function($reader) {
	        $sheet = $reader->getSheet(0);
	        $highestRow=$sheet->getHighestRow();//取得总行数
			$highestColumn=$sheet->getHighestColumn(); //取得总列数
			//循环读取excel文件,读取一条,插入一条
			for($j=1;$j<=$highestRow;$j++){//从第一行开始读取数据
				$str='';
				for($k='A';$k<=$highestColumn;$k++){            //从A列读取数据
				//这种方法简单，但有不妥，以'\\'合并为数组，再分割\\为字段值插入到数据库,实测在excel中，如果某单元格的值包含了\\导入的数据会为空        
				$data[$j][$k]=$sheet->getCell("$k$j")->getValue().'\\';//读取单元格
			 }
			}
			foreach ($data as $k => $v) {
				if($v['A'] == '\\')
				{
					dump($v['A']);
					break;
				}
				$idno = substr($v['A'], 0, -1);
				// $entry = DB::table('checkentry')->where('idno', $idno)->get();
				// if(!$entry->isEmpty())
				// {
				// 	dump($entry);
				// 	continue;
				// }
				// echo '---------';
				if($k == 2) {
					$check['entry'] = $v['B'];
					$check['code'] = $v['C'];
					$entryId = DB::table('checkentry')->insertGetId([
						'entry' => substr($v['B'], 0, -1) ,
						'code' => substr($v['C'], 0, -1),
						'idno' => substr($v['A'], 0, -1)
					]);
				}
				$j = $k - 1;
				if($k > 2) {
					if($v['A']!=$data[$j]['A']) {
						$check['entry'] = $v['B'];
						$check['code'] = $v['C'];
						$entryId = DB::table('checkentry')->insertGetId([
							'entry' => substr($v['B'], 0, -1) ,
							'code' => substr($v['C'], 0, -1),
							'idno' => substr($v['A'], 0, -1)
						]);
					}
				}
				if($k == 1 ) {
					continue;
				}
				//负面清单
				if($v['D']!='\\') {
					DB::table('checknegative')->insertGetId([
						'entryId' => $entryId,
						'content' => substr($v['D'], 0, -1)
					]);
				}
				//处罚标准
				if($v['G']!='\\') {

					DB::table('checkcommand')->insertGetId([
						'entryId' => $entryId,
						'title' => substr($v['G'], 0, -1),
						'content' => substr($v['H'], 0, -1)
					]);
				}
				//依据标准
				if($v['E']!='\\') {
					DB::table('checkaccord')->insertGetId([
						'entryId' => $entryId,
						'title' => substr($v['E'], 0, -1),
						'content' => substr($v['F'], 0, -1),
					]);
				}
				
			}
	    });
			
	}

	public function uploadNineExcel(UploadFileRequest $request)
	{
		set_time_limit(0);
		$file = $_FILES['file'];
		$fileName = $request->get('file_name');
		$fileName = $fileName ?: $file['name'];
		$path = str_finish($request->get('folder'), '/') . $fileName;
		$data = [];
		$content = File::get($file['tmp_name']);
		$filePath = $file['tmp_name'];
		// $filePath = './public/word/test.xlsx';
		Excel::load($filePath, function($reader) {
			$sheet = $reader->getSheet(0);
			$highestRow=$sheet->getHighestRow();//取得总行数
			$highestColumn=$sheet->getHighestColumn(); //取得总列数
			//循环读取excel文件,读取一条,插入一条
			for($j=1;$j<=$highestRow;$j++){//从第一行开始读取数据
				$str='';
				for($k='A';$k<=$highestColumn;$k++){            //从A列读取数据
					//这种方法简单，但有不妥，以'\\'合并为数组，再分割\\为字段值插入到数据库,实测在excel中，如果某单元格的值包含了\\导入的数据会为空
					$data[$j][$k]=$sheet->getCell("$k$j")->getValue().'\\';//读取单元格
				}
			}
			foreach ($data as $k => $v) {
				if($v['A'] == '\\')
				{
					dump($v['A']);
					break;
				}
				$statusarray = [
					'人' => 1,
					'物' => 2,
					'环' => 3,
					'管' => 4,
							];
				$typearray = [
					'是' => 1,
					'否' => 2,
				];
				$idno = substr($v['A'], 0, -1);
				if($k == 2) {
					$checkgroup['type'] = $typearray[substr($v['B'], 0, -1)];
					$checkgroup['status'] = $statusarray[substr($v['C'], 0, -1)];
					$checkgroup['categoryId'] = 10;
					$checkgroup['name'] = substr($v['D'], 0, -1);
					$checkgroup['groupNo'] = substr($v['A'], 0, -1);
					$checkgroup['accidentType'] = substr($v['J'], 0, -1);
					$checkgroup['riskPoint'] = substr($v['I'], 0, -1);
					$checkgroup['measures'] = substr($v['K'], 0, -1);
					$checkgroup['emergencyMeasure'] = substr($v['L'], 0, -1);
					var_dump($checkgroup);
					$groupId = DB::table('checkgroup')->insertGetId($checkgroup);
				}
				$j = $k - 1;
				if($k > 2) {
					if($v['A']!=$data[$j]['A']) {
					$checkgroup['type'] = $typearray[substr($v['B'], 0, -1)];
					$checkgroup['status'] = $statusarray[substr($v['C'], 0, -1)];
					$checkgroup['categoryId'] = 10;
					$checkgroup['name'] = substr($v['D'], 0, -1);
					$checkgroup['groupNo'] = substr($v['A'], 0, -1);
					$checkgroup['accidentType'] = substr($v['J'], 0, -1);
					$checkgroup['riskPoint'] = substr($v['I'], 0, -1);
					$checkgroup['measures'] = substr($v['K'], 0, -1);
					$checkgroup['emergencyMeasure'] = substr($v['L'], 0, -1);
					var_dump($checkgroup);
					$groupId = DB::table('checkgroup')->insertGetId($checkgroup);
						// $entryId = DB::table('checkentry')->insertGetId([
						// 	'entry' => substr($v['B'], 0, -1) ,
						// 	'code' => substr($v['C'], 0, -1),
						// 	'idno' => substr($v['A'], 0, -1)
						// ]);
					}
				}

				if($k == 1 ) {
					continue;
				}
				if($v['G']!=$data[$j]['G']) {
					$entryId = DB::table('checkentry')->insertGetId([
						'groupId' => $groupId,
						'entry' => substr($v['E'], 0, -1),
						'idno' => substr($v['G'], 0, -1)
					]);
					var_dump($entryId);
				}
				if($v['H']!='\\') {
					DB::table('checknegative')->insertGetId([
						'entryId' => $entryId,
						'content' => substr($v['H'], 0, -1),
						'necode' => substr($v['M'], 0, -1)
					]);
				}
				if($v['F']!='\\') {
					$accord['entryId'] = $entryId;
						$accord['title'] = substr($v['F'], 0, -1);
						DB::table('checkaccord')->insertGetId($accord);
				}
			}
		});

	}

	public function uploadNineExcelUpdate(UploadFileRequest $request)
	{
		set_time_limit(0);
		$file = $_FILES['file'];
		$fileName = $request->get('file_name');
		$fileName = $fileName ?: $file['name'];
		$path = str_finish($request->get('folder'), '/') . $fileName;
		$data = [];
		$content = File::get($file['tmp_name']);
		$filePath = $file['tmp_name'];
		// $filePath = './public/word/test.xlsx';
		Excel::load($filePath, function($reader) {
			$sheet = $reader->getSheet(0);
			$highestRow=$sheet->getHighestRow();//取得总行数
			$highestColumn=$sheet->getHighestColumn(); //取得总列数
			//循环读取excel文件,读取一条,插入一条
			for($j=1;$j<=$highestRow;$j++){//从第一行开始读取数据
				$str='';
				for($k='A';$k<=$highestColumn;$k++){            //从A列读取数据
					//这种方法简单，但有不妥，以'\\'合并为数组，再分割\\为字段值插入到数据库,实测在excel中，如果某单元格的值包含了\\导入的数据会为空
					$data[$j][$k]=$sheet->getCell("$k$j")->getValue().'\\';//读取单元格
				}
			}
			foreach ($data as $k => $v) {
				if($v['A'] == '\\')
				{
					dump($v['A']);
					break;
				}
				$statusarray = [
					'基础档案' => 1,
					'现场检查' => 2,
							];
				$typearray = [
					'消防安全' => 1,
					'生产安全' => 2,
				];
				$idno = substr($v['A'], 0, -1);
				$status = strpos($idno,'HD');

				$entryno = substr($v['G'], 0 ,-1);

				if($status === 0) {
					$category = substr($idno,2,2);
					$entryinfo = DB::table('checkentry')->where(['idno'=>$entryno])->first();
					if($entryinfo) {
						$entryId = $entryinfo->id;
						if($v['E']!='\\') {
							$accord['entryId'] = $entryId;
							$accord['title'] = substr($v['E'], 0, -1);
							$accord['content'] = substr($v['F'], 0, -1);
							$accord['ckano'] = substr($v['I'], 0, -1);
							DB::table('checkaccord')->insertGetId($accord);
						}
						if($v['H']!='\\') {
							DB::table('checknegative')->insertGetId([
								'entryId' => $entryId,
								'content' => substr($v['H'], 0, -1),
								'necode' => substr($v['I'], 0, -1)
								]);
						}
					} else {
						$groupinfo = DB::table('checkgroup')->where(['groupNo'=>$idno])->first();
						if($groupinfo) {
							$groupId = $groupinfo->id;
						} else {
							$checkgroup['type'] = $typearray[substr($v['B'], 0, -1)];
							$checkgroup['status'] = $statusarray[substr($v['C'], 0, -1)];
							$checkgroup['categoryId'] = $category;
							$checkgroup['name'] = substr($v['C'], 0, -1);
							$checkgroup['groupNo'] = substr($v['A'], 0, -1);
							var_dump($checkgroup);
							$groupId = DB::table('checkgroup')->insertGetId($checkgroup);
						}
						
					}
					$j = $k-1;
				if(!$entryinfo) {
					$entryId = DB::table('checkentry')->insertGetId([
						'groupId' => $groupId,
						'entry' => substr($v['D'], 0, -1),
						'idno' => substr($v['G'], 0, -1)
					]);
					var_dump($entryId);
					if($v['H']!='\\') {
						DB::table('checknegative')->insertGetId([
							'entryId' => $entryId,
							'content' => substr($v['H'], 0, -1),
							'necode' => substr($v['I'], 0, -1)
							]);
					}
					if($v['E']!='\\') {
							$accord['entryId'] = $entryId;
							$accord['title'] = substr($v['E'], 0, -1);
							$accord['content'] = substr($v['F'], 0, -1);
							$accord['ckano'] = substr($v['I'], 0, -1);
							DB::table('checkaccord')->insertGetId($accord);
						}
					
				}
				}
				
				
			}
		});

	}

	public function uploadNineExcelTz(UploadFileRequest $request)
	{
		set_time_limit(0);
		$file = $_FILES['file'];
		$fileName = $request->get('file_name');
		$fileName = $fileName ?: $file['name'];
		$path = str_finish($request->get('folder'), '/') . $fileName;
		$data = [];
		$content = File::get($file['tmp_name']);
		$filePath = $file['tmp_name'];
		// $filePath = './public/word/test.xlsx';
		Excel::load($filePath, function($reader) {
			$sheet = $reader->getSheet(0);
			$highestRow=$sheet->getHighestRow();//取得总行数
			$highestColumn=$sheet->getHighestColumn(); //取得总列数
			//循环读取excel文件,读取一条,插入一条
			for($j=1;$j<=$highestRow;$j++){//从第一行开始读取数据
				$str='';
				for($k='A';$k<=$highestColumn;$k++){            //从A列读取数据
					//这种方法简单，但有不妥，以'\\'合并为数组，再分割\\为字段值插入到数据库,实测在excel中，如果某单元格的值包含了\\导入的数据会为空
					$data[$j][$k]=$sheet->getCell("$k$j")->getValue().'\\';//读取单元格
				}
			}
			foreach ($data as $k => $v) {
				if($v['A'] == '\\')
				{
					dump($v['A']);
					break;
				}
				$statusarray = [
					'基础档案' => 1,
					'现场检查' => 2,
							];
				$typearray = [
					'消防安全' => 1,
					'生产安全' => 2,
				];
				$idno = substr($v['A'], 0, -1);
				$status = strpos($idno,'TZ');
				$entryno = substr($v['G'], 0 ,-1);
				if($status === 0) {
					$category = substr($idno,2,2);
					$entryinfo = DB::table('checkentry')->where(['idno'=>$entryno])->first();
					if($entryinfo) {
						$entryId = $entryinfo->id;
						if($v['E']!='\\') {
							$accord['entryId'] = $entryId;
							$accord['title'] = substr($v['E'], 0, -1);
							$accord['content'] = substr($v['F'], 0, -1);
							$accord['ckano'] = substr($v['I'], 0, -1);
							DB::table('checkaccord')->insertGetId($accord);
						}
						if($v['H']!='\\') {
							DB::table('checknegative')->insertGetId([
								'entryId' => $entryId,
								'content' => substr($v['H'], 0, -1),
								'necode' => substr($v['I'], 0, -1)
								]);
						}
					} else {
						$groupinfo = DB::table('checkgroup')->where(['groupNo'=>$idno])->first();
						if($groupinfo) {
							$groupId = $groupinfo->id;
						} else {
							$checkgroup['type'] = 5;
							$checkgroup['status'] = $statusarray[substr($v['C'], 0, -1)];
							$checkgroup['categoryId'] = $category;
							$checkgroup['name'] = substr($v['C'], 0, -1);
							$checkgroup['groupNo'] = substr($v['A'], 0, -1);
							var_dump($checkgroup);
							$groupId = DB::table('checkgroup')->insertGetId($checkgroup);
						}
						
					}
					$j = $k-1;
				if(!$entryinfo) {
					$entryId = DB::table('checkentry')->insertGetId([
						'groupId' => $groupId,
						'entry' => substr($v['D'], 0, -1),
						'idno' => substr($v['G'], 0, -1)
					]);
					var_dump($entryId);
					if($v['H']!='\\') {
						DB::table('checknegative')->insertGetId([
							'entryId' => $entryId,
							'content' => substr($v['H'], 0, -1),
							'necode' => substr($v['I'], 0, -1)
							]);
					}
					if($v['E']!='\\') {
							$accord['entryId'] = $entryId;
							$accord['title'] = substr($v['E'], 0, -1);
							$accord['content'] = substr($v['F'], 0, -1);
							$accord['ckano'] = substr($v['I'], 0, -1);
							DB::table('checkaccord')->insertGetId($accord);
						}
					
				}
				}
				
				
			}
		});

	}

	public function uploadNineExcelDel(UploadFileRequest $request)
	{
		set_time_limit(0);
		$file = $_FILES['file'];
		$fileName = $request->get('file_name');
		$fileName = $fileName ?: $file['name'];
		$path = str_finish($request->get('folder'), '/') . $fileName;
		$data = [];
		$content = File::get($file['tmp_name']);
		$filePath = $file['tmp_name'];
		// $filePath = './public/word/test.xlsx';
		Excel::load($filePath, function($reader) {
			$sheet = $reader->getSheet(0);
			$highestRow=$sheet->getHighestRow();//取得总行数
			$highestColumn=$sheet->getHighestColumn(); //取得总列数
			//循环读取excel文件,读取一条,插入一条
			for($j=1;$j<=$highestRow;$j++){//从第一行开始读取数据
				$str='';
				for($k='A';$k<=$highestColumn;$k++){            //从A列读取数据
					//这种方法简单，但有不妥，以'\\'合并为数组，再分割\\为字段值插入到数据库,实测在excel中，如果某单元格的值包含了\\导入的数据会为空
					$data[$j][$k]=$sheet->getCell("$k$j")->getValue().'\\';//读取单元格
				}
			}
			$group = [];
			$entry = [];
			foreach ($data as $k => $v) {
				if($v['D']!='\\') {
					$group[] = $v['A'];
				}
				if($v['E']!='\\') {
					$entry[] = $v['H'];
				}
				
			}
			foreach ($entry as $k => $v) {
				$v = substr($v, 0, -1);
				DB::table('checkentry')->where('idno',$v)->update(['isdel'=>2]);
			}
			foreach ($group as $k => $v) {
				$v = substr($v, 0, -1);
				DB::table('checkgroup')->where('groupNo',$v)->update(['isdel'=>2]);
			}
			dd($entry);
		dd($group);
		});
		

	}

	public function updateNagative()
	{
		set_time_limit(0);
		$file = $_FILES['file'];
		$fileName = $request->get('file_name');
		$fileName = $fileName ?: $file['name'];
		$path = str_finish($request->get('folder'), '/') . $fileName;
		$data = [];
		$content = File::get($file['tmp_name']);
		$filePath = $file['tmp_name'];
		// $filePath = './public/word/test.xlsx';
		Excel::load($filePath, function($reader) {
			$sheet = $reader->getSheet(0);
			$highestRow=$sheet->getHighestRow();//取得总行数
			$highestColumn=$sheet->getHighestColumn(); //取得总列数
			//循环读取excel文件,读取一条,插入一条
			for($j=1;$j<=$highestRow;$j++){//从第一行开始读取数据
				$str='';
				for($k='G';$k<=$highestColumn;$k++){            //从A列读取数据
					$data[$j][$k]=$sheet->getCell("$k$j")->getValue().'\\';//读取单元格
				}
			}
			dd($data);
			foreach ($data as $k => $v) {
				if($v['A'] == '\\')
				{
					dump($v['A']);
					break;
				}
				$statusarray = [
					'人' => 1,
					'物' => 2,
					'环' => 3,
					'管' => 4,
							];
				$typearray = [
					'是' => 1,
					'否' => 2,
				];
				$idno = substr($v['A'], 0, -1);
				if($k == 2) {
					$checkgroup['type'] = $typearray[substr($v['B'], 0, -1)];
					$checkgroup['status'] = $statusarray[substr($v['C'], 0, -1)];
					$checkgroup['categoryId'] = 1;
					$checkgroup['name'] = substr($v['D'], 0, -1);
					var_dump($checkgroup);
					$groupId = DB::table('checkgroup')->insertGetId($checkgroup);
				}
				$j = $k - 1;
				if($k > 2) {
					if($v['A']!=$data[$j]['A']) {
					$checkgroup['type'] = $typearray[substr($v['B'], 0, -1)];
					$checkgroup['status'] = $statusarray[substr($v['C'], 0, -1)];
					$checkgroup['categoryId'] = 1;
					$checkgroup['name'] = substr($v['D'], 0, -1);
					var_dump($checkgroup);
					$groupId = DB::table('checkgroup')->insertGetId($checkgroup);
						// $entryId = DB::table('checkentry')->insertGetId([
						// 	'entry' => substr($v['B'], 0, -1) ,
						// 	'code' => substr($v['C'], 0, -1),
						// 	'idno' => substr($v['A'], 0, -1)
						// ]);
					}
				}

				if($k == 1 ) {
					continue;
				}
				if($v['E']!='\\') {
					$id = DB::table('checkentry')->insertGetId([
						'groupId' => $groupId,
						'entry' => substr($v['E'], 0, -1),
						'idno' => substr($v['F'], 0, -1)
					]);
					var_dump($id);
				}
				if($v['F']!='\\') {
					$accord['entryId'] = $id;
						$accord['title'] = substr($v['F'], 0, -1);
						DB::table('checkaccord')->insertGetId($accord);
				}
			}
		});
	}

	public function updateAccord(UploadFileRequest $request)
	{
		set_time_limit(0);
		$file = $_FILES['file'];
		$fileName = $request->get('file_name');
		$fileName = $fileName ?: $file['name'];
		$path = str_finish($request->get('folder'), '/') . $fileName;
		$data = [];
		$content = File::get($file['tmp_name']);
		$filePath = $file['tmp_name'];
		// $filePath = './public/word/test.xlsx';
		Excel::load($filePath, function($reader) {
			$sheet = $reader->getSheet(0);
			$highestRow=$sheet->getHighestRow();//取得总行数
			$highestColumn=$sheet->getHighestColumn(); //取得总列数
			//循环读取excel文件,读取一条,插入一条
			for($j=1;$j<=$highestRow;$j++){//从第一行开始读取数据
				$str='';
				for($k='A';$k<=$highestColumn;$k++){            //从A列读取数据
					$data[$j][$k]=$sheet->getCell("$k$j")->getValue().'\\';//读取单元格
				}
			}
			$accord = [];
			$k = 1;
			foreach ($data as $k => $v) {
				$idno = substr($v['G'], 0, -1);
				$gno = substr($v['A'],0,-1);
				if($idno) {
					$j = $k - 1;
					if($v['G']!=$data[$j]['G']) {
						$checkentry = DB::select("select * from checkentry where idno = '{$idno}'");
						$accord = substr($v['F'],0,-1);
						Checkaccord::where(['entryId'=>$checkentry[0]->id])->update(['title'=>$accord]);
					}
					
				}
				if($gno) {
					$j = $k - 1;
					if($v['A']!=$data[$j]['A']) {
						$checkentry = DB::select("select * from checkgroup where groupNo = '{$gno}'");
						$group['measures'] = substr($v['K'],0,-1);
						$group['emergencyMeasure'] = substr($v['L'],0,-1);
						Checkgroup::where(['groupNo'=>$gno])->update($group);
					}
				}
				
			
			}
		});
	}

	public function uploadImg(Request $request)
	{
		// header("Access-Control-Allow-Origin: *");
		$upload = new UploadClass(); 

		$upload->exts=array('jpg','png'); 

		$upload->maxSize=15*1024*1024; 
		$upload->savePath='storage/images'; 
		$file = $request->file('fileImg'); 
		$host = $_SERVER['HTTP_HOST'];

		$imgUrl = $upload->upload($file); 
		$client = new \GuzzleHttp\Client();
		$avatarResponse = $client->get('http://'.$host.$imgUrl);

		$img = Image::make($avatarResponse->getBody()->getContents())->resize(400, 600);

		$newimg = substr($imgUrl,1);
		$newimgUrl = str_replace(".","_23.",$newimg);
		// var_dump($upload->getError());
		// $img = Image::make($newimg)->resize(800, 600);
		$img->save($newimgUrl);
		return response()->json(['data' => $imgUrl,'code' => 200], 200);
	}

	public function uploadMp4(Request $request)
	{
		$upload = new UploadClass(); 
		$upload->exts=array('mp4'); 
		$upload->maxSize=15*1024*1024; 
		$upload->savePath='storage/mp4'; 
		$file = $request->file('file'); 
		$imgUrl = $upload->upload($file); 
		if(!$imgUrl){
			$imgUrl = $upload->getError();
		}
		return response()->json(['data' => $imgUrl,'code' => 200], 200)->setEncodingOptions(JSON_UNESCAPED_UNICODE);
	}

	public function getMap()
	{
		$ak = '4cba5031276db6a671290687e176a350';
		$url = 'https://restapi.amap.com/v3/staticmap?location=120.370839,36.128151&zoom=13&size=750*300&markers=mid,,A:120.370839,36.128151&key='.$ak;
		$image = file_get_contents($url);
		$imgname=rand(1000,10000).time().'.png';
		$path = 'images/'.date('Ymd').'/'.$imgname;
        Storage::put($path,$image);
		Storage::disk('public')->put($imgname,$image);
		dd($path);
	}

	public function upsignature()
	{
		$data=$request->all();
		$imgdata=$imgdata['imgurl'];
		$base64_str = substr($imgdata, strpos($imgdata, ",")+1);
		$image=base64_decode($base64_str);
		$imgname=rand(1000,10000).time().'.png';
		Storage::disk('public')->put($imgname,$image);
	}

	/**
	 * 上传文件
	 */
	public function uploadFile(UploadFileRequest $request)
	{
	    $file = $_FILES['file'];
	    $fileName = $request->get('file_name');
	    $fileName = $fileName ?: $file['name'];
	    $path = str_finish($request->get('folder'), '/') . $fileName;
	    $content = File::get($file['tmp_name']);

	    $result = $this->manager->saveFile($path, $content);

	    if ($result === true) {
	        return redirect()
	            ->back()
	            ->with("success", '文件「' . $fileName . '」上传成功.');
	    }

	    $error = $result ?: "文件上传出错.";
	    return redirect()
	        ->back()
	        ->withErrors([$error]);
	}

}
